<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme

export default {
  props: {
    //   从组件调用者传进来的样式类
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    // 折线图的数据项
    chartData: {
      type: Object,
      required: true,
    },
    // 折线图的标题
    chartTitle: {
      type: String,
      default: "",
    },
  },

  data() {
    return {
      chart: null,
    };
  },
  // 组件属性监视
  watch: {
    // 监视chart的数据
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  //   销毁chart组件
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ data_1, data_2 } = {}) {
      this.chart.setOption({
        title: {
          text: this.chartTitle,
          textStyle: {
            color: "#000",
            fontStyle: "normal",
            fontSize: 18,
          },
          textAlign: "center",
          padding: 10,
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          axisTick: {
            show: true,
          },
        },
        yAxis: {
          axisLine: {
            show: true,
          },
        },
        legend: {},
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ["line", "bar"] },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        series: [
          {
            name: data_1.name,
            //   平滑样式
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 2,
                },
                areaStyle: {
                  color: "#f3f8ff",
                },
              },
            },
            data: data_1.data,
            animationDuration: 2800,
            animationEasing: "quadraticOut",
          },
          {
            name: data_2.name,
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#FF005A",
                lineStyle: {
                  color: "#FF005A",
                  width: 2,
                },
              },
            },
            data: data_2.data,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ],
      });
    },
  },
};
</script>

<style>
</style>